package cn.woblog.recyclerviewsample.divider;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.State;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import cn.woblog.recyclerviewsample.R;

/**
 * Created by Pich on 17/4/5.
 */

public class CustomGridItemDecoration extends RecyclerView.ItemDecoration {

  private final Context context;
  private Drawable divider;

  public CustomGridItemDecoration(Context context) {
    this.context = context;
    divider = context.getResources().getDrawable(R.drawable.item_divider);
  }

  @Override
  public void onDraw(Canvas c, RecyclerView parent, State state) {
    super.onDraw(c, parent, state);
    draw(c, parent, state);
  }

  private void draw(Canvas c, RecyclerView parent, State state) {
    int childCount = parent.getChildCount();
    int left = 0;
    int top = 0;
    int right = 0;
    int bottom = 0;
    for (int i = 0; i < childCount; i++) {
      View view = parent.getChildAt(i);
      //垂直
      left = view.getLeft();
      top = view.getBottom();
      right = view.getRight();
      bottom = top + divider.getIntrinsicHeight();
      divider.setBounds(left, top, right, bottom);
      divider.draw(c);

      //水平
      left = view.getRight();
      top = view.getTop();
      right = left + divider.getIntrinsicHeight();
      bottom = view.getBottom();
      divider.setBounds(left, top, right, bottom);
      divider.draw(c);
    }
  }

  @Override
  public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
    int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
    int spanCount = getSpanCount(parent);
    int childCount = parent.getAdapter().getItemCount();

    boolean isLastRow = isLastRow(parent, itemPosition, spanCount, childCount);
    boolean isLastColumn = isLastColumn(parent, itemPosition, spanCount, childCount);

    int left;
    int top;
    int right;
    int bottom;

    int itemWidth =
        (parent.getWidth() - (spanCount - 1) * divider.getIntrinsicHeight()) / spanCount;
    int i = itemPosition % spanCount;

    left = i * (itemWidth + divider.getIntrinsicHeight());
    top = 0;
    right = left + itemWidth + divider.getIntrinsicHeight();

    if (isLastColumn) {
      right -= divider.getIntrinsicHeight();
    }

    if (isLastRow) {
      bottom = 0;
    } else {
      bottom = divider.getIntrinsicHeight();
    }

    outRect.set(left, top, right, bottom);
    Log.d("TAG", left + "，" + top + "," + right + "," + bottom);

  }


  private boolean isLastRow(RecyclerView parent, int pos, int spanCount,
      int childCount) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
      // childCount = childCount - childCount % spanCount;
      int lines = childCount % spanCount == 0 ? childCount / spanCount : childCount / spanCount + 1;
      return lines == pos / spanCount + 1;
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
      int orientation = ((StaggeredGridLayoutManager) layoutManager)
          .getOrientation();
      // StaggeredGridLayoutManager 且纵向滚动
      if (orientation == StaggeredGridLayoutManager.VERTICAL) {
        childCount = childCount - childCount % spanCount;
        // 如果是最后一行，则不需要绘制底部
        if (pos >= childCount) {
          return true;
        }
      } else {
        // 如果是最后一行，则不需要绘制底部
        if ((pos + 1) % spanCount == 0) {
          return true;
        }
      }
    }
    return false;
  }

  private boolean isLastColumn(RecyclerView parent, int pos, int spanCount,
      int childCount) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
      if ((pos + 1) % spanCount == 0) {// 如果是最后一列，则不需要绘制右边
        return true;
      }
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
      int orientation = ((StaggeredGridLayoutManager) layoutManager)
          .getOrientation();
      if (orientation == StaggeredGridLayoutManager.VERTICAL) {
        if ((pos + 1) % spanCount == 0)// 如果是最后一列，则不需要绘制右边
        {
          return true;
        }
      } else {
        childCount = childCount - childCount % spanCount;
        if (pos >= childCount)// 如果是最后一列，则不需要绘制右边
        {
          return true;
        }
      }
    }
    return false;
  }

  private int getSpanCount(RecyclerView parent) {
    // 列数
    int spanCount = -1;
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {

      spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
      spanCount = ((StaggeredGridLayoutManager) layoutManager)
          .getSpanCount();
    }
    return spanCount;
  }
}
